class UserProduct {
  final String id;
  final String userId; //外键，指向 User
  final String productId; //外键，指向 Product
  final DateTime purchaseDate;

  UserProduct({
    required this.id,
    required this.userId,
    required this.productId,
    required this.purchaseDate,
  });

  factory UserProduct.fromMap(Map<String, dynamic> map) {
    return UserProduct(
      id: map['id'] as String,
      userId: map['userId'] as String,
      productId: map['productId'] as String,
      //从数据库存储的TEXT(ISO 8601格式)转换回DateTime对象
      purchaseDate: DateTime.parse(map['purchaseDate'] as String),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'userId': userId,
      'productId': productId,
      //将DateTime对象转换为ISO 8601格式的字符串，以便存入SQLite
      'purchaseDate': purchaseDate.toIso8601String(),
    };
  }
}